package com.ziyueyuwen.xiniao.controller.product.order;

import com.suyeer.basic.util.HttpResUtil;
import com.ziyueyuwen.xiniao.base.HttpResContent;
import com.ziyueyuwen.xiniao.bean.ZyOrderAddress;
import com.ziyueyuwen.xiniao.bean.product.OrderAddressSaveAO;
import com.ziyueyuwen.xiniao.enums.ParamTypeEnum;
import com.ziyueyuwen.xiniao.service.baseservice.order.ZyOrderAddressService;
import com.ziyueyuwen.xiniao.valid.group.interfaces.address.AddressSave;
import com.ziyueyuwen.xiniao.valid.group.interfaces.address.AddressUpdate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

import static com.ziyueyuwen.xiniao.base.BaseUtil.*;
import static com.ziyueyuwen.xiniao.util.ConstUtil.*;

/**
 * @author jun 2019/2/25
 */
@Controller
@RequestMapping("/product/order/address")
@Validated
public class ProductOrderAddressController {

    @Autowired
    ZyOrderAddressService zyOrderAddressService;

    @RequestMapping(value = "/get_address_list.do", method = RequestMethod.POST)
    public void getAddressList(HttpServletRequest request, HttpServletResponse response) {
        HttpResContent<List<ZyOrderAddress>> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("获取当前用户地址列表");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            List<ZyOrderAddress> data = zyOrderAddressService.getListByCreateUserAccount(userAccount);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/get_address_by_id.do", method = RequestMethod.POST)
    public void getAddressById(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam(PARAM_PRODUCT_ORDER_ADDRESS_ID) Integer addressId) {
        HttpResContent<ZyOrderAddress> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("根据id获取地址信息");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_PRODUCT_ORDER_ADDRESS_ID, addressId);
            ZyOrderAddress data = zyOrderAddressService.selectByPreKey(addressId);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/save_address.do", method = RequestMethod.POST)
    public void saveAddress(HttpServletRequest request, HttpServletResponse response,
                            @Validated(AddressSave.class) OrderAddressSaveAO address) {
        HttpResContent<ZyOrderAddress> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("添加地址");
        try {
            String userAccount = hrc.getUserInfo().getUserAccount();
            ZyOrderAddress save = new ZyOrderAddress();
            save.setConsigneeAccount(address.getConsigneeAccount())
                    .setConsignee(address.getConsignee())
                    .setArea(address.getArea())
                    .setAddress(address.getAddress())
                    .setCreateUserAccount(userAccount)
                    .setCreateTime(new Date());
            zyOrderAddressService.insertUseGeneratedKeys(save);
            saveSuccessMsgAndData(hrc, save);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/update_address.do", method = RequestMethod.POST)
    public void updateAddress(HttpServletRequest request, HttpServletResponse response,
                              @Validated(AddressUpdate.class) OrderAddressSaveAO address) {
        HttpResContent<ZyOrderAddress> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("修改地址");
        try {
            ZyOrderAddress update = new ZyOrderAddress();
            update.setConsigneeAccount(address.getConsigneeAccount())
                    .setConsignee(address.getConsignee())
                    .setArea(address.getArea())
                    .setAddress(address.getAddress())
                    .setId(address.getId());
            Boolean data = zyOrderAddressService.update(update);
            update = zyOrderAddressService.selectByPreKey(address.getId());
            saveSuccessMsgAndData(hrc, update);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

    @RequestMapping(value = "/delete_address.do", method = RequestMethod.POST)
    public void deleteAddress(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam(PARAM_PRODUCT_ORDER_ADDRESS_ID) Integer addressId) {
        HttpResContent<Boolean> hrc = new HttpResContent<>(request, response);
        hrc.setFuncDes("删除地址");
        try {
            checkParam(ParamTypeEnum.INTEGER_GT_0, PARAM_PRODUCT_ORDER_ADDRESS_ID, addressId);
            Boolean data = zyOrderAddressService.deleteByPreKey(addressId);
            saveSuccessMsgAndData(hrc, data);
        } catch (Exception e) {
            saveErrorMsgAndData(hrc, e);
        } finally {
            HttpResUtil.writeResult(hrc, true);
        }
    }

}
